/**
 * 
 */
package de.tum.in.eist.poll.client.views;

import java.util.ArrayList;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import de.tum.in.eist.poll.client.controllers.Controller;
import de.tum.in.eist.poll.client.views.widgets.questions.QuestionCreationSingleChoiceWidget;
import de.tum.in.eist.poll.client.views.widgets.questions.QuestionCreationTextWidget;
import de.tum.in.eist.poll.client.views.widgets.questions.QuestionCreationWidget;
import de.tum.in.eist.poll.shared.beans.Question;
import de.tum.in.eist.poll.shared.beans.QuestionGroup;

/**
 * The Lecture Mangagement Page
 * 
 * @author Sebastian Hofstetter
 * 
 */
public class LectureManagementView extends View {

	private VerticalPanel lectureManagementPanel = new VerticalPanel();
	private TextBox lectureNameTextBox = new TextBox();
	private Button textButton = new Button("Add Text-Question");
	private Button singleChoiceButton = new Button("Add Single-Choice-Question");
	private Button submitButton = new Button("Add lecture");
	private VerticalPanel questionPanel = new VerticalPanel();
	private HorizontalPanel buttonPanel = new HorizontalPanel();

	private ArrayList<QuestionCreationWidget> questions = new ArrayList<QuestionCreationWidget>();

	public LectureManagementView() {
		lectureNameTextBox.setText("lecture name");
		lectureManagementPanel.add(lectureNameTextBox);
		
		// addbuttons
		buttonPanel.add(singleChoiceButton);
		buttonPanel.add(textButton);
		lectureManagementPanel.add(buttonPanel);

		// textbutton
		textButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				QuestionCreationTextWidget tmp = new QuestionCreationTextWidget();
				questions.add(tmp);
				questionPanel.add(tmp);
			}
		});

		// singleChoiceButton
		singleChoiceButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				QuestionCreationSingleChoiceWidget tmp = new QuestionCreationSingleChoiceWidget();
				questions.add(tmp);
				questionPanel.add(tmp);
			}
		});
		lectureManagementPanel.add(questionPanel);

		submitButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				if (lectureNameTextBox.getText() != "" && questions.size() > 0) {
					Question[] q = new Question[questions.size()];
					for(int i = 0; i<questions.size(); i++) {
						q[i] = questions.get(i).getQuestion();
					}
					
					Controller.lectureManagementController.addLectures(lectureNameTextBox.getText(), new QuestionGroup(q));
				}
			}
		});
		
		lectureManagementPanel.add(submitButton);
		
		initWidget(lectureManagementPanel);
	}

}
